package com.example.starapp.luckfrag;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.starapp.R;
import com.example.starapp.utils.HttpUtils;
import com.example.starapp.utils.LoadNetDataAsyncTask;
import com.example.starapp.utils.URLUtils;
import com.google.gson.Gson;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

public class FortuneActivity extends AppCompatActivity implements View.OnClickListener , LoadNetDataAsyncTask.onGetNetDataListener{
    private static final String TAG = "FortuneActivity";
    TextView zhTv,loveTv,syTv,cfTv,jkTv,titleTv;
    ImageView ivBack;
    String star_name;
    String luckUrl;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fortune);
        zhTv=findViewById(R.id.luck_zh_tv);
        loveTv=findViewById(R.id.luck_love_tv);
        syTv=findViewById(R.id.luck_sy_tv);
        cfTv=findViewById(R.id.luck_cf_tv);
        jkTv=findViewById(R.id.luck_jk_tv);
        titleTv=findViewById(R.id.title_tv);
        ivBack=findViewById(R.id.title_iv_back);
        ivBack.setOnClickListener(this);
        getLastData();//接收数据
        titleTv.setText(star_name);

        try {
            String encode = URLEncoder.encode(star_name, "UTF-8");
            luckUrl = URLUtils.getLuckUrl(encode);//得到网址
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        new LoadNetDataAsyncTask(this,this).execute(luckUrl);  //开始网络请求
    }

    private void getLastData() {
        Intent intent = getIntent();
        star_name = intent.getStringExtra("star_name");
        Log.d(TAG, "star_name: "+star_name);

    }



    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.title_iv_back:
                finish();
                break;
        }
    }

    @Override
    public void showDataFromNet(String s) {
        ShowData(s);
    }
    private void ShowData(String json) {
        LuckInfo luckInfo = new Gson().fromJson(json, LuckInfo.class);
        List<String> career = luckInfo.getCareer();
        String shiye = career.get(0);
        syTv.setText(shiye);
        Log.d(TAG, "shiye: "+shiye);
        String zh = luckInfo.getMima().getText().get(0);
        zhTv.setText(zh);
        String love = luckInfo.getLove().get(0);
        loveTv.setText(love);
        String jk = luckInfo.getHealth().get(0);
        jkTv.setText(jk);
        String cf = luckInfo.getFinance().get(0);
        cfTv.setText(cf);
    }
}